package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态Controller
 *
 * @author jjp
 * @date 2020-04-16
 */

@RestController
@Api(description = "User动态")
@RequestMapping("/mobileUser/dynamic")
public class UserDynamicController extends BaseController {
    @Autowired
    private ISysDynamicService sysDynamicService;

    @Autowired
    private ISysDynamicFileService sysDynamicFileService;

    @Autowired
    private ISysDynamicCommentService sysDynamicCommentService;

    @Autowired
    private ISysUserPariseService sysUserPariseService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;


    @Autowired
    private ISysDynamicPariseService sysDynamicPariseService;
    @Autowired
    private ISysFollowService sysFollowService;
    @Autowired
    private ISysMomentCollectionService sysMomentCollectionService;
    @Autowired
    private ISysMomentService momentService;
    @Autowired
    private ISysMomentFileService sysMomentFileService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysPariseService sysPariseService;
    @Autowired
    private ISysMomentCommentService sysMomentCommentService;


    /**
     * 查询动态列表
     */

    @ApiOperation("查询所有人动态列表")
    @GetMapping("/getAllList")
    public TableDataInfo getAllList(SysDynamic sysDynamic) {
        startPage();
        sysDynamic.setDelFlag("0");
        List<SysDynamic> list = sysDynamicService.selectSysDynamicList(sysDynamic);
        for (int i = 0; i < list.size(); i++) {
            Long mobileUserId = list.get(i).getMobileUserId();
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
            List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(list.get(i).getDynamicId());
            SysDynamicComment sysDynamicComment = new SysDynamicComment();
            sysDynamicComment.setDynamicId(sysDynamic.getDynamicId());
            List<SysDynamicComment> sysDynamicComments = sysDynamicCommentService.selectSysDynamicCommentList(sysDynamicComment);
            list.get(i).setSysMobileUser(sysMobileUser);
            list.get(i).setDynamicFileList(sysDynamicFiles);
            list.get(i).setCommentTotal(sysDynamicComments.size());
        }
        return getDataTable(list);
    }


    /**
     * 查询动态列表
     */

    @ApiOperation("查询自己动态列表")
    @GetMapping("/getMyList")
    public TableDataInfo getMyList(SysDynamic sysDynamic) {
        startPage();
        sysDynamic.setDelFlag("0");
        sysDynamic.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        List<SysDynamic> list = sysDynamicService.selectSysDynamicList(sysDynamic);
        for (int i = 0; i < list.size(); i++) {
            Long dynamicId = list.get(i).getDynamicId();
            List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
            list.get(i).setDynamicFileList(sysDynamicFiles);
            SysDynamicParise sysDynamicParise = new SysDynamicParise();
            sysDynamicParise.setDynamicId(dynamicId);
            List<SysDynamicParise> pariseList = sysDynamicPariseService.selectSysDynamicPariseList(sysDynamicParise);
            list.get(i).setPariseTotal(pariseList.size());
            List<SysDynamicComment> commentList = sysDynamicCommentService.findDynamicId(dynamicId);
            list.get(i).setCommentTotal(commentList.size());
        }
        return getDataTable(list);
    }


    /**
     * 查询动态列表
     */

    @ApiOperation("查询别人动态列表")
    @GetMapping("/getOthersDynamicList")
    public TableDataInfo getOthersDynamicList(SysMobileUser sysMobileUser) {
        SysDynamic sysDynamic = new SysDynamic();
        startPage();
        sysDynamic.setDelFlag("0");
        sysDynamic.setMobileUserId(sysMobileUser.getMobileUserId());
        List<SysDynamic> list = sysDynamicService.selectSysDynamicList(sysDynamic);
        for (int i = 0; i < list.size(); i++) {
            Long dynamicId = list.get(i).getDynamicId();
            List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
            list.get(i).setDynamicFileList(sysDynamicFiles);
            SysDynamicParise sysDynamicParise = new SysDynamicParise();
            sysDynamicParise.setDynamicId(dynamicId);
            List<SysDynamicParise> pariseList = sysDynamicPariseService.selectSysDynamicPariseList(sysDynamicParise);
            list.get(i).setPariseTotal(pariseList.size());
            List<SysDynamicComment> commentList = sysDynamicCommentService.findDynamicId(dynamicId);
            list.get(i).setCommentTotal(commentList.size());
        }
        return getDataTable(list);
    }

    @ApiOperation("查询别人信息")
    @GetMapping("/getPersonInformation")
    public AjaxResult getPersonInformation(SysMobileUser sysMobileUser) {
        SysMobileUser mobileUser = sysMobileUserService.selectSysMobileUserById(sysMobileUser.getMobileUserId());
        SysDynamicParise sysDynamicParise = new SysDynamicParise();
        sysDynamicParise.setMobileUserId(sysMobileUser.getMobileUserId());
        List<SysDynamicParise> pariseList = sysDynamicPariseService.selectSysDynamicPariseList(sysDynamicParise);
        SysFollow sysFollow = new SysFollow();
        sysFollow.setUserId(sysMobileUser.getMobileUserId());
        List<SysFollow> followList = sysFollowService.selectSysFollowList(sysFollow);
        SysFollow follow = new SysFollow();
        sysFollow.setFensiUserId(sysMobileUser.getMobileUserId());
        List<SysFollow> followList1 = sysFollowService.selectSysFollowList(follow);
        Map<String, Object> map = new HashMap<>();
        map.put("mobileUser", mobileUser);
        map.put("pariseSize", pariseList.size());
        map.put("fanSize", followList.size());
        map.put("followSize", followList1.size());
        return AjaxResult.success(map);
    }

    @ApiOperation("查询自己信息")
    @GetMapping("/getMyInformation")
    public AjaxResult getMyInformation(SysMobileUser sysMobileUser) {
        SysMobileUser mobileUser = sysMobileUserService.selectSysMobileUserById(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        SysDynamicParise sysDynamicParise = new SysDynamicParise();
        sysDynamicParise.setMobileUserId(sysMobileUser.getMobileUserId());
        List<SysDynamicParise> pariseList = sysDynamicPariseService.selectSysDynamicPariseList(sysDynamicParise);
        SysFollow sysFollow = new SysFollow();
        sysFollow.setUserId(sysMobileUser.getMobileUserId());
        List<SysFollow> followList = sysFollowService.selectSysFollowList(sysFollow);
        SysFollow follow = new SysFollow();
        sysFollow.setFensiUserId(sysMobileUser.getMobileUserId());
        List<SysFollow> followList1 = sysFollowService.selectSysFollowList(follow);
        Map<String, Object> map = new HashMap<>();
        map.put("mobileUser", mobileUser);
        map.put("pariseSize", pariseList.size());
        map.put("fanSize", followList.size());
        map.put("followSize", followList1.size());
        return AjaxResult.success(map);
    }

    /**
     * 查询别人收藏平台瞬间列表
     */
    @ApiOperation("查询别人收藏平台瞬间列表")
    @GetMapping("/getMomentCollection")
    public TableDataInfo getMomentCollection(SysMomentCollection sysMomentCollection) {
        startPage();
        List<SysMomentCollection> list = sysMomentCollectionService.selectSysMomentCollectionList(sysMomentCollection);
        for (int i = 0; i < list.size(); i++) {
            Long momentId = list.get(i).getMomentId();
            SysMoment sysMoment = momentService.selectSysMomentById(momentId);
            List<SysMomentFile> sysMomentFiles = sysMomentFileService.selectSysMomentId(momentId);
            SysUser sysUser = sysUserService.selectUserGetUserName(sysMoment.getCreateBy());
            sysMoment.setSysUser(sysUser);
            sysMoment.setSysMomentFile(sysMomentFiles);
            SysParise sysParise = new SysParise();
            sysParise.setMomentId(momentId);
            List<SysParise> sysParises = sysPariseService.selectSysParise(sysParise);
            sysMoment.setPariseTotal(sysParises.size());
            List<SysMomentComment> sysMomentCommentList = sysMomentCommentService.selectSysMomentId(momentId);
            sysMoment.setCommentTotal(sysMomentCommentList.size());
            list.get(i).setSysMoment(sysMoment);
        }
        return getDataTable(list);
    }


    /**
     * 获取动态详细信息
     */

    @ApiOperation("获取动态详细信息")
    @GetMapping(value = "/getInfo/{dynamicId}")
    public AjaxResult getInfo(@PathVariable("dynamicId") Long dynamicId) {
        SysDynamic sysDynamic = sysDynamicService.selectSysDynamicById(dynamicId);
        SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysDynamic.getMobileUserId());
        List<SysDynamicFile> sysDynamicFiles = sysDynamicFileService.selectSysDynamicFileByDynamicId(dynamicId);
        SysDynamicComment sysDynamicComment = new SysDynamicComment();
        sysDynamicComment.setDynamicId(dynamicId);
        List<SysDynamicComment> sysDynamicComments = sysDynamicCommentService.selectSysDynamicCommentList(sysDynamicComment);
        for (int i = 0; i < sysDynamicComments.size(); i++) {
            Long mobileUserId = sysDynamicComments.get(i).getMobileUserId();
            Long commentId = sysDynamicComments.get(i).getCommentId();
            SysMobileUser mobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
            sysDynamicComments.get(i).setSysMobileUser(mobileUser);
            SysDynamicComment dynamicComment = new SysDynamicComment();
            dynamicComment.setParentCommentId(commentId);
            List<SysDynamicComment> dynamicCommentList = sysDynamicCommentService.selectSysDynamicCommentList(dynamicComment);
            sysDynamicComments.get(i).setTotal(dynamicCommentList.size());
        }

        SysDynamicParise sysDynamicParise = new SysDynamicParise();
        sysDynamicParise.setDynamicId(dynamicId);
        List<SysDynamicParise> pariseList = sysDynamicPariseService.selectSysDynamicPariseList(sysDynamicParise);


        sysDynamic.setSysMobileUser(sysMobileUser);
        sysDynamic.setDynamicFileList(sysDynamicFiles);
        sysDynamic.setDynamicCommentList(sysDynamicComments);
        sysDynamic.setPariseTotal(pariseList.size());
        sysDynamic.setCommentTotal(sysDynamicComments.size());

        return AjaxResult.success(sysDynamic);
    }


    /**
     * 获取动态评论详细信息
     */

    @ApiOperation("获取动态评论详细信息")
    @GetMapping(value = "/getComment/{commentId}")
    public AjaxResult getComment(@PathVariable("commentId") Long commentId) {
        SysDynamicComment dynamicComment = new SysDynamicComment();
        dynamicComment.setParentCommentId(commentId);
        List<SysDynamicComment> dynamicCommentList = sysDynamicCommentService.selectSysDynamicCommentList(dynamicComment);
        for (int i = 0; i < dynamicCommentList.size(); i++) {
            Long mobileUserId = dynamicCommentList.get(i).getMobileUserId();
            SysMobileUser mobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
            dynamicCommentList.get(i).setSysMobileUser(mobileUser);
        }
        return AjaxResult.success(dynamicCommentList);
    }


    /**
     * 新增动态
     */

    @Log(title = "动态", businessType = BusinessType.INSERT)
    @ApiOperation("新增动态")
    @PostMapping
    public AjaxResult add(@RequestBody SysDynamic sysDynamic) {
        sysDynamic.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        sysDynamic.setDelFlag("0");
        int i = sysDynamicService.insertSysDynamic(sysDynamic);
        List<String> urls = sysDynamic.getUrls();
        Long dynamicId = sysDynamic.getDynamicId();
        if (urls == null) {
            return toAjax(i);
        } else {
            for (int j = 0; j < urls.size(); j++) {
                SysDynamicFile sysDynamicFile = new SysDynamicFile();
                String url = urls.get(j);
                sysDynamicFile.setDynamicId(dynamicId);
                sysDynamicFile.setUrl(url);
                sysDynamicFileService.insertSysDynamicFile(sysDynamicFile);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改动态
     */

    @Log(title = "动态", businessType = BusinessType.UPDATE)
    @ApiOperation("修改动态")
    @PutMapping
    public AjaxResult edit(@RequestBody SysDynamic sysDynamic) {
        int i = sysDynamicService.updateSysDynamic(sysDynamic);
        List<String> urls = sysDynamic.getUrls();
        Long dynamicId = sysDynamic.getDynamicId();
        if (urls == null) {
            return toAjax(i);
        } else {
            sysDynamicFileService.deleteSysDynamicFileByDynamicId(dynamicId);

            for (int j = 0; j < urls.size(); j++) {
                SysDynamicFile sysDynamicFile = new SysDynamicFile();
                String url = urls.get(j);
                sysDynamicFile.setDynamicId(dynamicId);
                sysDynamicFile.setUrl(url);
                sysDynamicFileService.insertSysDynamicFile(sysDynamicFile);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 删除动态
     */

    @ApiOperation("删除动态")
    @Log(title = "动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dynamicId}")
    public AjaxResult remove(@PathVariable Long dynamicId) {
        SysDynamic sysDynamic = new SysDynamic();
        sysDynamic.setDelFlag("1");
        sysDynamic.setDynamicId(dynamicId);
        return toAjax(sysDynamicService.updateSysDynamic(sysDynamic));
    }
}
